﻿var grid, cbProgress;

//初始化表格
function InitGrid() {
    var txtGOODSCODE = $('#txtGOODSCODE').val();
    var txtGOODSNAME = $('#txtGOODSNAME').val();
    var selecCellNo = $('#cellNo').combobox('getValue');
    var selectSaleDays = $('#saleDays').combobox('getValue');

    grid = $('#tab_list').datagrid({
        name: 'tbl_data',
        url: '/LocationInventoryBatch/InitJsonResult',
        title: '货位商品库存信息',
        queryParams: {
            goodsCode: txtGOODSCODE,
            goodsName: txtGOODSNAME,
            cellNo: selecCellNo,
            saleDays: selectSaleDays
        },
        //width: 815,
        //height: 530,
        //border : false,
        //fit: false,
        fitColumns: false,
        //nowrap: true,
        rownumbers: true,
        //showFooter: true,
        //loadMsg: '正在加载信息...',
        //frozen: true,
        //sortName: "CODE", //排序设置
        singleSelect: true,
        idField: 'LOCATIONINVENTORYID',
        columns: [[
        //{ field: 'ck', checkbox: true },
                    { field: 'LOCATIONINVENTORYID', title: '货位库存ID', hidden: true },
                    { field: 'GOODSBATCHCODE', title: '商品批次号', align: 'center', sortable: true },
                    { field: 'LOCATIONCODE', title: '货位编码', align: 'center', sortable: true },
                    { field: 'GOODSCODE', title: '商品编码', align: 'center', sortable: true },
                    { field: 'GOODSNAME', title: '商品名称', align: 'center', sortable: true },
                    { field: 'WAREHOUSENAME', title: '仓库', align: 'center', sortable: true },
                    { field: 'CELLNO', title: '库位', align: 'center', sortable: true },
                    { field: 'QTY', title: '库存数', align: 'center', sortable: true },
                    { field: 'UNITNAME', title: '单位', align: 'center', sortable: true },
                    { field: 'PRODUCTIONDATE', title: '生产日期', align: 'center', sortable: true },
                    { field: 'EXPIRYDAYS', title: '保质天数', align: 'center', sortable: true },
                    { field: 'DIFFDAYS', title: '已过天数', align: 'center', sortable: true },
                    { field: 'OVERDAYS', title: '临保天数', align: 'center', sortable: true},
                    { field: 'SALEDAYS', title: '解锁天数', align: 'center', sortable: true },
                    { field: 'QUALITYSTATUS', title: '状态', align: 'center', sortable: true, formatter: formatQualityStatus }
        ]],
        toolbar: [{
            id: 'btnEdit',
            text: '修改',
            iconCls: 'icon-edit',
            handler: function () {
                edit();
            }
        }],
        pagination: true,
        pageSize: 20,
        pageNumber: 1,
        pageList: [10, 20, 30, 40, 50],
        rownumbers: true, //行号
        onLoadSuccess: function (data) {
            onRefreshButton();
        },
        onSelect: function (rowIndex, rowData) {
            onRefreshButton();
        },
        onUnselect: function (rowIndex, rowData) {
            onRefreshButton();
        },
        onSortColumn: function (sort, order) {
            grid.datagrid('reload');
        }
    });
    //清空选择
    grid.datagrid('clearSelections');
}

function formatQualityStatus(val) {
    if (val != null) {
        if (val == "1") {
            return "临保";
        } else {
            return "有效";
        }
    }

    return "";
}

//控制按钮
function onRefreshButton() {
    $('#btnAdd').linkbutton('enable');
    $('#btnEdit').linkbutton('disable');
    $('#btnRemove').linkbutton('disable');

    var rows = grid.datagrid('getSelections');
    if (rows.length == 1) {
        $('#btnEdit').linkbutton('enable');
    }
}

//修改
function edit() {
    var row = grid.datagrid('getSelections');
    $('#dlg').dialog('open').dialog('setTitle', '编辑货位商品关联');
    var a = row.GOODSCODE;
    $('#txtGoodsCode').val(row[0].GOODSCODE);
    $('#txtGoodsName').val(row[0].GOODSNAME);
    $('#hiddenInventoryId').val(row[0].LOCATIONINVENTORYID);
    $('#hiddenOverdays').val(row[0].OVERDAYS);

    $('#txtGoodsCode').attr("readonly", true);
    $('#txtGoodsName').attr("readonly", true);
    url = '/LocationInventoryBatch/SaveData';

}

function save() {

    var txtNumber = $('#txtNumber').val();
    var hiddenOverdays = $('#hiddenOverdays').val();
    
    if (eval(txtNumber) >= eval(hiddenOverdays)) {
        $.messager.alert('提示', '解锁天数不能大于等于临保天数！', 'warning');
        return false;
    }

    $('#location').form('submit', {
        url: '/LocationInventoryBatch/SaveData',
        success: function (data) {
            eval('data=' + data);
            if (data.success) {
                $('#dlg').dialog('close');
                InitGrid();
            } else {
                $.messager.alert('错误', data.msg, 'error');
                //alert("保存失败！请检查");
            }
        }
    });
}


//提交参数
function submitParams() {
    $('#fm2').form('submit', {
        url: '/Distr/UpdateParams',
        success: function (data) {
            eval('data=' + data);
            if (data.success) {
                grid.datagrid('reload');
                $.messager.alert('提示', '参数修改成功!', 'info');
            } else {
                $.messager.alert('错误', data.msg, 'error');
            }
        }
    });
}

